package com.GroupSix.service;

import com.GroupSix.dao.ClassScoreDao;
import com.GroupSix.dao.ExamAnswerDao;
import com.GroupSix.entity.ClassScoreEntity;
import com.GroupSix.entity.ExamAnswerEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

@Service("ClassScoreService")
public class ClassScoreServiceImpl extends ServiceImpl<ClassScoreDao, ClassScoreEntity>
        implements ClassScoreService {

    //注入答题卡
    @Autowired
    private ExamAnswerDao examAnswerDao;

    //注入班级平均分表
    @Autowired
    private ClassScoreDao classScoreDao;

    //统计所有班级平均分
    @Override
    public String average(){
        int sum = examAnswerDao.selectCount(new QueryWrapper<>());//已有的答题卡数量
        for(int i=1;i<=sum;i++){
            //当前答题卡
            QueryWrapper<ExamAnswerEntity> qwExam = new QueryWrapper<>();
            qwExam.eq("id",i);
            ExamAnswerEntity examAnswerEntity = examAnswerDao.selectOne(qwExam);
            if(examAnswerEntity.getIsUsed()==0) {//判断该答题卡成绩是否统计过
                int courseId = examAnswerEntity.getCourseId();
                //当前答题卡对应的班级
                QueryWrapper<ClassScoreEntity> qwClass = new QueryWrapper<>();
                qwClass.eq("class_name", examAnswerEntity.getClassId());
                ClassScoreEntity classScoreEntity = classScoreDao.selectOne(qwClass);
                double score = examAnswerEntity.getScore();
                double avg;//该科目的平均分
                int num;//该科目已经统计数量
                Collection<Integer> newStuName = new ArrayList<>();//参加该科目考试的学生姓名
                Collection<Integer> newStuScore = new ArrayList<>();//该科目的成绩单
                switch (courseId) {
                    case 11://该答题卡为近代史
                        avg = classScoreEntity.getCourse1AverageScore();
                        num = classScoreEntity.getCourse1Num();
                        //更新学生名单
                        Integer[] stuName1 = classScoreEntity.getCourse1Student();
                        Collections.addAll(newStuName, stuName1);
                        newStuName.add(examAnswerEntity.getStudentId());
                        classScoreEntity.setCourse1Student(newStuName.toArray(new Integer[newStuName.size()]));
                        //更新成绩单
                        Integer[] stuScore1 = classScoreEntity.getCourse1Score();
                        Collections.addAll(newStuScore, stuScore1);
                        newStuScore.add((int) Math.round(examAnswerEntity.getScore()));
                        classScoreEntity.setCourse1Score(newStuScore.toArray(new Integer[newStuScore.size()]));
                        //计算新的该科目平均分
                        classScoreEntity.setCourse1AverageScore((avg * num + score) / (++num));
                        classScoreEntity.setCourse1Num(num);
                        classScoreDao.update(classScoreEntity, qwClass);
                        break;
                    case 12://该答题卡为高数
                        avg = classScoreEntity.getCourse2AverageScore();
                        num = classScoreEntity.getCourse2Num();
                        //更新学生名单
                        Integer[] stuName2 = classScoreEntity.getCourse2Student();
                        Collections.addAll(newStuName, stuName2);
                        newStuName.add(examAnswerEntity.getStudentId());
                        classScoreEntity.setCourse2Student(newStuName.toArray(new Integer[newStuName.size()]));
                        //更新成绩单
                        Integer[] stuScore2 = classScoreEntity.getCourse2Score();
                        Collections.addAll(newStuScore, stuScore2);
                        newStuScore.add((int) Math.round(examAnswerEntity.getScore()));
                        classScoreEntity.setCourse2Score(newStuScore.toArray(new Integer[newStuScore.size()]));
                        //计算新的该科目平均分
                        classScoreEntity.setCourse2AverageScore((avg * num + score) / (++num));
                        classScoreEntity.setCourse2Num(num);
                        classScoreDao.update(classScoreEntity, qwClass);
                        break;
                    case 13://该答题卡为Java
                        avg = classScoreEntity.getCourse3AverageScore();
                        num = classScoreEntity.getCourse3Num();
                        //更新学生名单
                        Integer[] stuName3 = classScoreEntity.getCourse3Student();
                        Collections.addAll(newStuName, stuName3);
                        newStuName.add(examAnswerEntity.getStudentId());
                        classScoreEntity.setCourse3Student(newStuName.toArray(new Integer[newStuName.size()]));
                        //更新成绩单
                        Integer[] stuScore3 = classScoreEntity.getCourse3Score();
                        Collections.addAll(newStuScore, stuScore3);
                        newStuScore.add((int) Math.round(examAnswerEntity.getScore()));
                        classScoreEntity.setCourse3Score(newStuScore.toArray(new Integer[newStuScore.size()]));
                        //计算新的该科目平均分
                        classScoreEntity.setCourse3AverageScore((avg * num + score) / (++num));
                        classScoreEntity.setCourse3Num(num);
                        classScoreDao.update(classScoreEntity, qwClass);
                        break;
                }
                examAnswerEntity.setIsUsed(1);
                examAnswerDao.updateById(examAnswerEntity);//将统计标记更新为1，表示已经统计过
            }
        }
        return "统计平均分成功";
    }
}
